package parttern.flyweight;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class TicketFactory {

    private final static Map<String, Ticket> ticketMap = new ConcurrentHashMap<>();

    public static Ticket getTicket(String from, String to) {
        String key = from + "-" + to;
        if (ticketMap.containsKey(key)) {
            System.out.println("使用缓存==>" + key);
            return ticketMap.get(key);
        }
        System.out.println("创建对象");
        Ticket ticket = new TrainTicket(from, to);
        ticketMap.put(key, ticket);
        return ticket;
    }

    ///test
    public static void main(String[] args) {
        Ticket ticket1 = TicketFactory.getTicket("纽约", "巴黎");
        ticket1.showTicketInfo("上铺");
        Ticket ticket12 = TicketFactory.getTicket("纽约", "巴黎");
        ticket12.showTicketInfo("中铺");
        Ticket ticket3 = TicketFactory.getTicket("纽约", "巴黎");
        ticket3.showTicketInfo("下铺");

        //看看String常量池
        String a = "zhangsan";
        String b = "lisi";
        String c = "zhangsan";
        System.out.println("a.equals(b):" + a.equals(b));
        System.out.println("a.equals(c):" + a.equals(c));
        System.out.println("a.hashCode():" + a.hashCode());
        System.out.println("b.hashCode():" + b.hashCode());
        System.out.println("c.hashCode():" + c.hashCode());
    }

}
